{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ef14f135",
   "metadata": {},
   "source": [
    "# 第九周学习笔记\n",
    "\n",
    "[dict](https://docs.python.org/3/tutorial/datastructures.html#dictionaries)\n",
    "\n",
    "* 练习\n",
    "```\n",
    ">>> tel = {'jack': 4098, 'sape': 4139}\n",
    ">>> tel['guido'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'sape': 4139, 'guido': 4127}\n",
    ">>> tel['jack']\n",
    "4098\n",
    ">>> del tel['sape']\n",
    ">>> tel['irv'] = 4127\n",
    ">>> tel\n",
    "{'jack': 4098, 'guido': 4127, 'irv': 4127}\n",
    ">>> list(tel)\n",
    "['jack', 'guido', 'irv']\n",
    ">>> sorted(tel)\n",
    "['guido', 'irv', 'jack']\n",
    ">>> 'guido' in tel\n",
    "True\n",
    ">>> 'jack' not in tel\n",
    "False\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5a450894",
   "metadata": {},
   "source": [
    "## 字典的查询\n",
    ">1.in方法查询       \n",
    ">- 描述    \n",
    ">  Python 字典 in 操作符用于判断键是否存在于字典中，如果键在字典 dict 里返回 true，否则返回 false。                   \n",
    ">  而 not in 操作符刚好相反，如果键在字典 dict 里返回 false，否则返回 true。     \n",
    ">- 语法         \n",
    ">  in 操作符语法：      \n",
    ">  key in dict         \n",
    ">- 参数     \n",
    ">  key -- 要在字典中查找的键。\n",
    ">- 返回值    \n",
    ">  如果键在字典里返回true，否则返回false。\n",
    ">\n",
    ">2.get方法获取\n",
    ">- 描述  \n",
    ">  Python 字典 get() 函数返回指定键的值。  \n",
    ">- 语法  \n",
    ">  get()方法语法：  \n",
    ">  dict.get(key, default=None)  \n",
    ">- 参数  \n",
    ">  key -- 字典中要查找的键。  \n",
    ">  default -- 如果指定的键不存在时，返回该默认值。  \n",
    ">- 返回值  \n",
    ">  返回指定键的值，如果键不在字典中返回默认值 None 或者指定的默认值。   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "05a3e854",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "e81f8516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 查询 key 是否在字典当中 用in\n",
    "\"stu1801\" in info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "856c6f89",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# get 方法获取key对应的value内容\n",
    "info.get(\"stu1801\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "cfecac6b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "ZiQi\n"
     ]
    }
   ],
   "source": [
    "# 综上所述：字典的查询方法一般步骤如下：\n",
    "if \"stu1801\" in info:\n",
    "    print(info.get(\"stu1801\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "03116532",
   "metadata": {},
   "source": [
    "## 字典的删除\n",
    ">1.pop()指定删除           \n",
    ">2.gpopitem()随机删除    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "c8779700",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "ab5064bd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'ZiQi'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.pop('stu1801')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "81542bd0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "f5217acb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('stu1803', 'LiuYu')"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.popitem()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "24b12fbb",
   "metadata": {},
   "source": [
    "## 字典的一般方法\n",
    "> 1.字典的循环遍历   \n",
    "> 2.fromkeys()方法   \n",
    "> 3.setdefault()方法   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "5ab99d9c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}\n"
     ]
    }
   ],
   "source": [
    "info = {\n",
    "    \"stu1801\":\"ZiQi\",\n",
    "    \"stu1802\":\"XiaoFeng\",\n",
    "    \"stu1803\":\"LiuYu\"\n",
    "}\n",
    "\n",
    "print(info)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9cbe2925",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi', 'stu1802': 'XiaoFeng', 'stu1803': 'LiuYu'}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6e042858",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1801 ZiQi\n",
      "1802 XiaoFeng\n",
      "1803 LiuYu\n"
     ]
    }
   ],
   "source": [
    "# 循环遍历字典\n",
    "# 循环遍历的最大用户就是：给所有的值使用同样的操作/方法\n",
    "stu_id_list = []\n",
    "for k,v in info.items():\n",
    "    print(k[3:],v)\n",
    "    stu_id_list.append(k[3:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "2e70b563",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['1801', '1802', '1803']"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stu_id_list"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a824b25c",
   "metadata": {},
   "source": [
    "* 练习：请大家把info的 value目前的格式进行统一修改，修改的范式为：姓 名 Zi Qi  Xiao Feng"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ee855e97",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function fromkeys:\n",
      "\n",
      "fromkeys(iterable, value=None, /) method of builtins.type instance\n",
      "    Create a new dictionary with keys from iterable and values set to value.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.fromkeys)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "d738709c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': None, 'age': None, 'sex': None}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化字典信息\n",
    "stu1801_detail={}\n",
    "stu1801_detail.fromkeys([\"name\",\"age\",\"sex\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "dea95618",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stud2101': 123456, 'stud2102': 123456, 'stud2103': 123456}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 初始化字典信息\n",
    "学生初始密码={}\n",
    "学生初始密码.fromkeys([\"stud2101\",\"stud2102\",\"stud2103\"],123456)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "56790376",
   "metadata": {},
   "source": [
    "setdefault()方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "f9625f26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function setdefault:\n",
      "\n",
      "setdefault(key, default=None, /) method of builtins.dict instance\n",
      "    Insert key with a value of default if key is not in the dictionary.\n",
      "    \n",
      "    Return the value for key if key is in the dictionary, else default.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(info.setdefault)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "cd5b797c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'hongjia'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info.setdefault('stu1805','hongjia')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "2ef1fd28",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'stu1801': 'ZiQi',\n",
       " 'stu1802': 'XiaoFeng',\n",
       " 'stu1803': 'LiuYu',\n",
       " 'stu1805': 'hongjia'}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "info"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5227cde3",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
